package com.myqm.util.gateway;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON; 

public class PluginsGateway {

	static String url = "http://222.196.35.4:8001";
	
	public static PluginsInfo addApiPlugin(PluginsInfo plugin, String apiName) throws Exception {
		String path = url + "/plugins";
		plugin.setName("acl");
		plugin.setEnabled(true);
		String api_id = ApiGateway.getApi(apiName).getId(); 
		plugin.setApi_id(api_id);
		plugin.setConfig(apiPlugins(plugin.getName()));
		System.out.println(path);
		String result = "";
		result = ApiGatewayNetOp.netNotByGET(path, plugin, "POST");
		System.out.println(result);
		PluginsInfo newPlugin = JSON.parseObject(result, PluginsInfo.class);
		System.out.println(newPlugin);
		return plugin;
	}

	public static Object addConsumerPlugin(String consumerName, String pluginName) throws Exception {
		String path = url + "/consumers";
		if(consumerName != null)
			path += "/" + consumerName;
		if(pluginName != null)
			path += "/" + pluginName;
		System.out.println(path);
		Object plugin = null;
		plugin = PluginsGateway.consumerPlugins(pluginName);
		String result = "";
		result = ApiGatewayNetOp.netNotByGET(path, plugin, "POST");
		System.out.println(result);
		return plugin;
	}
	
	public static PluginsInfo getPlugin(String id) throws Exception {
		String path = url + "/plugins";
		if(id != null) {
			path += "/" + id;
		}
		System.out.println(path);
		String result = "";
		result = ApiGatewayNetOp.netByGET(path, null);
		PluginsInfo plugin = JSON.parseObject(result, PluginsInfo.class);
		System.out.println(plugin);
		return plugin;	
	}
	
	public static PluginsList getPlugins() throws Exception {
		String path = url + "/plugins";
		System.out.println(path);
		String result = "";
		result = ApiGatewayNetOp.netByGET(path, null);
		PluginsList plugins = JSON.parseObject(result, PluginsList.class);
		System.out.println(plugins);
		return plugins;		
	}
	
	public static PluginsInfo delPlugin(String id) throws Exception {
		String path = url + "/plugins";
		if(id != null) {
			path += "/" + id;
		}
		String result = "";
		System.out.println(path);
		result = ApiGatewayNetOp.netNotByGET(path, null, "DELETE");
		PluginsInfo plugin = JSON.parseObject(result, PluginsInfo.class);
		System.out.println(plugin);
		return plugin;
	}	
	
	public static void main(String[] args) {
		try {
			//PluginsGateway.addApiPlugin(new PluginsInfo(), "baidu");
			PluginsGateway.addConsumerPlugin("user123", "acls");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static Object apiPlugins(String pluginName) {
		if(pluginName == "acl") {
			AclInfo acl = new AclInfo();
			List<String> whitelist = new ArrayList<String>();
			whitelist.add("testblack");
			acl.setWhitelist(whitelist);
			return acl;
		}else if(pluginName == "jwt") {
			JwtInfo jwt = new JwtInfo();
			jwt.setSecret_is_base64(true);
			jwt.setRun_on_preflight(true);
			jwt.setKey_claim_name("iss");
			List<String> claims_to_verify = new ArrayList<String>();
			claims_to_verify.add("exp");//"exp",  "nbf"
			jwt.setClaims_to_verify(claims_to_verify);
			List<String> uri_param_names = new ArrayList<String>();
			uri_param_names.add("jwt");
			jwt.setUri_param_names(uri_param_names);
			return jwt;
		}else {			//pluginsName == "basic-auth"
			BasicAuthInfo basicAuth = new BasicAuthInfo();
			basicAuth.setHide_credentials(true);
			return basicAuth;
		}
	}
	
	private static Object consumerPlugins(String pluginName) {
		if(pluginName == "basic-auth") {
			ConsumerBasicAuth basicAuth = new ConsumerBasicAuth();
			basicAuth.setUsername("123");
			basicAuth.setPassword("123");
			return basicAuth;
		}else if(pluginName == "jwt") {
			ConsumerJwt jwt = new ConsumerJwt();
			jwt.setKey("iss");
			jwt.setSecret("123");
			return jwt;
		}else {
			ConsumerAcls acls = new ConsumerAcls();
			acls.setGroup("testblack");
			return acls;
		}	
	}
}
